/*
 * Copyright 2005-2013 finalist.cn. All rights reserved.
 * Support: http://www.finalist.cn
 * License: http://www.finalist.cn/license
 */
package com.finalist.dao.impl;

import java.util.Collections;
import java.util.List;

import javax.persistence.FlushModeType;
import javax.persistence.TypedQuery;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.finalist.dao.BossAreaDao;
import com.finalist.entity.BossArea;
import com.finalist.entity.BossArea.Type;

/**
 * Dao - BOSS地区
 * 
 * @author Finalist IT Group
 * @version 3.0
 */
@Repository("bossAreaDaoImpl")
public class BossAreaDaoImpl extends BaseDaoImpl<BossArea, Long> implements BossAreaDao
{
    private static final Log log = LogFactory.getLog(BossAreaDaoImpl.class);

    public List<BossArea> findRoots(Integer count)
    {
        String jpql = "select area from BossArea area where area.parent is null order by area.order asc";
        TypedQuery<BossArea> query = entityManager.createQuery(jpql, BossArea.class).setFlushMode(FlushModeType.COMMIT);
        if (count != null)
        {
            query.setMaxResults(count);
        }
        return query.getResultList();
    }

    public BossArea findInfoOfNoPrefix(String noPrefix)
    {
        if (noPrefix == null)
        {
            return null;
        }
        String jpql = "select area from BossArea area where lower(area.noPrefix) = lower(:noPrefix)";

        TypedQuery<BossArea> query = entityManager.createQuery(jpql, BossArea.class).setFlushMode(FlushModeType.COMMIT).setParameter("noPrefix", noPrefix);

        BossArea resultBossArea = null;

        if (query != null && CollectionUtils.isNotEmpty(query.getResultList()))
        {
            resultBossArea = query.getResultList().get(0);
        }
        return resultBossArea;
    }

    public BossArea findInfoOfNoPrefix(String noPrefix, Type noPrefixType)
    {
        if (noPrefix == null)
        {
            return null;
        }
        String jpql = "select area from BossArea area where lower(area.noPrefix) like lower(:noPrefix) and area.type=:noPrefixType and area.canCharge = :canCharge";

        TypedQuery<BossArea> query = entityManager.createQuery(jpql, BossArea.class).setFlushMode(FlushModeType.COMMIT).setParameter("noPrefix", noPrefix+"%").setParameter("noPrefixType", noPrefixType).setParameter("canCharge", true);

        BossArea resultBossArea = null;
        if (query != null && CollectionUtils.isNotEmpty(query.getResultList()))
        {
            resultBossArea = query.getResultList().get(0);
        }
        return resultBossArea;
    }

    @Override
    public List<BossArea> findPrefixByCityName(String name)
    {
        if (name == null)
        {
            return Collections.<BossArea> emptyList();
        }
        String jpql = "select bossArea from BossArea bossArea where lower(bossArea.name) = lower(:name)";
        return entityManager.createQuery(jpql, BossArea.class).setFlushMode(FlushModeType.COMMIT).setParameter("name", name).getResultList();
    }
    @Override
    public BossArea findByNoPrefix(String noPrefix)
    {
    	 if (noPrefix == null)
         {
             return null;
         }
         String jpql = "select distinct area from BossArea area where lower(area.noPrefix) = lower(:noPrefix)";

         TypedQuery<BossArea> query = entityManager.createQuery(jpql, BossArea.class).setFlushMode(FlushModeType.COMMIT).setParameter("noPrefix", noPrefix);

         BossArea resultBossArea = null;
         if (query != null && CollectionUtils.isNotEmpty(query.getResultList()))
         {
             resultBossArea = query.getResultList().get(0);
         }
         return resultBossArea;
    }
    @Override
    public BossArea findByPrefix(String prefix)
    {
    	 if (prefix == null)
         {
             return null;
         }
         try
         {
             String jpql = "select bossarea from BossArea bossArea where lower(bossArea.noPrefix) like lower(:noPrefix)";
             List<BossArea> lists = entityManager.createQuery(jpql, BossArea.class).setFlushMode(FlushModeType.COMMIT).setParameter("noPrefix", prefix +"%").getResultList();
             	if(lists!=null&&lists.size()>0){
             		return lists.get(0);
             		}
             return null;
             
         }
         catch (Exception e)
         {
         	e.printStackTrace();
         	log.error(e.toString());
             return null;
         }
    }

    @Override
    public List<BossArea> findByAllProvince()
    {
        try
        {
            String jpql = "select bossarea from BossArea bossArea where bossArea.id IN(SELECT DISTINCT parent FROM BossArea)";
            return entityManager.createQuery(jpql, BossArea.class).setFlushMode(FlushModeType.COMMIT).getResultList();
        }
        catch (Exception e)
        {
        	e.printStackTrace();
        	log.error(e.toString());
            return null;
        }
    }

    @Override
    public BossArea findCodeBySelectedCity(String selectedCity,BossArea.Type type)
    {
        if (selectedCity == null)
        {
            return null;
        }
        try
        {
            String jpql = "select bossarea from BossArea bossarea where bossArea.name = :name and bossArea.type = :type and rownum=1";
            return entityManager.createQuery(jpql, BossArea.class).setParameter("name", selectedCity).setParameter("type", type).getSingleResult();
        }
        catch (Exception e)
        {
        	e.printStackTrace();
        	log.error(e.toString());
            return null;
        }
    }

    @Override
    public BossArea findCityByCityCode(String cityCode,BossArea.Type type)
    {
        if (cityCode == null)
        {
            return null;
        }
        try
        {
            String jpql = "select bossarea from BossArea bossarea where bossarea.code = :code and rownum=1 and bossarea.type = :type ";
            return entityManager.createQuery(jpql, BossArea.class).setParameter("code", cityCode).setParameter("type", type).getSingleResult();
        }
        catch (Exception e)
        {
        	e.printStackTrace();
        	log.error(e.toString());
            return null;
        }
    }

    
    @Override
    public BossArea findByParentId(Long parentId)
    {
        if (parentId == null)
        {
            return null;
        }
        try
        {
            String jpql = "select bossarea from BossArea bossarea where bossArea.id = :parentId";
            return entityManager.createQuery(jpql, BossArea.class).setParameter("parentId", parentId).getSingleResult();
        }
        catch (Exception e)
        {
        	e.printStackTrace();
        	log.error(e.toString());
            return null;
        }
    }

    @Override
    public String findCodeByCityName(String cityName)
    {
        if (cityName == null)
        {
            return null;
        }
        try
        {
            String sql = "select distinct(code) from boss_area where name = '" + cityName + "'";
            return entityManager.createNativeQuery(sql).getSingleResult().toString();
        }
        catch (Exception e)
        {
        	e.printStackTrace();
        	log.error(e.toString());
            return null;
        }
    }

    @Override
    public String findCodeByName(String cityName)
    {
        if (cityName == null)
        {
            return null;
        }
        try
        {
            String sql = "select distinct(id) from boss_area where name = '" + cityName + "'";
            return entityManager.createNativeQuery(sql).getSingleResult().toString();
        }
        catch (Exception e)
        {
        	e.printStackTrace();
        	log.error(e.toString());
            return null;
        }
    }
    
    @Override
    public List<BossArea> findBossAreaByType(Type type)
    {
        String jpql = "select bossarea from BossArea bossarea where bossArea.type = :type ";
        return entityManager.createQuery(jpql, BossArea.class).setParameter("type", type).getResultList();
    }

	@Override
	public List<BossArea> findBossAreaByCityCode(String cityCode) {
		String jpql = "select bossarea from BossArea bossarea where bossArea.code = :cityCode1 ";
        List<BossArea> cityList = entityManager.createQuery(jpql, BossArea.class).setParameter("cityCode1", cityCode).getResultList();
        return cityList;
	}

	@Override
	public BossArea findByPrefix(String prefix, Boolean canCharge) {
		if (prefix == null)
        {
            return null;
        }
        try
        {
            String jpql = "select bossarea from BossArea bossArea where lower(bossArea.noPrefix) like lower(:noPrefix) and bossArea.canCharge = :canCharge";
            List<BossArea> lists = entityManager.createQuery(jpql, BossArea.class).setFlushMode(FlushModeType.COMMIT).setParameter("noPrefix", prefix +"%").setParameter("canCharge", canCharge).getResultList();
            	if(lists!=null&&lists.size()>0){
            		return lists.get(0);
            		}
            return null;
            
        }
        catch (Exception e)
        {
        	e.printStackTrace();
        	log.error(e.toString());
            return null;
        }
	}
    

	
}